package com.mystudy.management.usermanagement.service;


import com.mystudy.management.usermanagement.dao.mappers.VisitorMapper;
import com.mystudy.management.usermanagement.dao.pojo.Visitor;
import com.mystudy.management.usermanagement.dao.pojo.VisitorExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

//业务层申明
@Service("indexService")
//声明事务
@Transactional(
        isolation = Isolation.REPEATABLE_READ//事务隔离级别：可重复读
)
public class IndexServiceImpl implements IndexService {


    private VisitorMapper visitorMapper;

    @Autowired
    public void setVisitorMapper(VisitorMapper visitorMapper) {
        this.visitorMapper = visitorMapper;
    }

    public VisitorMapper getVisitorMapper() {
        return visitorMapper;
    }




    //----------------------------授权管理->授权管理--------------------------
    @Override//定义查询所有员工的方法
    public Collection<Visitor> selectAllVisitors() {
        List<Visitor> visitors = this.getVisitorMapper().selectByExample(null);
        for (Visitor visitor : visitors) {
            visitor.setPassword(null);
        }
        return visitors;
    }

    @Override//信息存在性查询
    public boolean messageCheck(Visitor visitor) {
        VisitorExample example = new VisitorExample();
        int resultNum = 0;
        if(visitor.getUserId() != null){
            example.createCriteria()
                    .andUserIdEqualTo(visitor.getUserId());
            List<Visitor> visitors = this.getVisitorMapper().selectByExample(example);
            resultNum = visitors.size();
        }else if(visitor.getUserName() != null){
            example.createCriteria()
                    .andUserNameEqualTo(visitor.getUserName());
            List<Visitor> visitors = this.getVisitorMapper().selectByExample(example);
            resultNum = visitors.size();
        }

        if(resultNum == 0){
            return false;
        }else{
            return true;
        }

    }

    //根据id或者name查询
    public List<Visitor> selectVisitorByExistCondition(Visitor visitor){
        List<Visitor> result = new ArrayList<>();
        System.out.println("数据库是否查到有关内容："+this.messageCheck(visitor));
        if (this.messageCheck(visitor)){
            VisitorExample example = new VisitorExample();
            if(visitor.getUserId() != null){            //  id查询
                result.add( this.getVisitorMapper().selectByPrimaryKey(visitor.getUserId()));
                return result;
            }else {//否则用户名查询
                example.createCriteria().andUserNameEqualTo(visitor.getUserName());
                return this.getVisitorMapper().selectByExample(example);
            }

        }else {
            return null;
        }
    }

    //删除操作

    @Override
    public boolean deleteVisitorById(Integer id){
        VisitorMapper visitorMapper = this.getVisitorMapper();
        //先查有没有此人
        if(visitorMapper.selectByPrimaryKey(id) != null){
            //执行删除操作
            visitorMapper.deleteByPrimaryKey(id);
            return true;
        }
        return false;//否则查询失败
    }


    //修改操作
    @Override
    public boolean updateVisiterMessage(Visitor visitor){
        VisitorMapper visitorMapper = this.getVisitorMapper();
        if(visitorMapper.selectByPrimaryKey(visitor.getUserId()) != null){
            VisitorExample example = new VisitorExample();
            example.createCriteria().andUserIdEqualTo(visitor.getUserId());
            visitor.setUserId(null);
            visitorMapper.updateByExampleSelective(visitor,example);
            return true;
        }else{
            return false;
        }
    }

    //添加操作
    @Override
    public boolean addVisitor(Visitor visitor){
        VisitorMapper mapper = this.getVisitorMapper();
        VisitorExample example = new VisitorExample();
        example.createCriteria().andUserNameEqualTo(visitor.getUserName());
        if(mapper.selectByExample(example).size() != 0){
            return false;
        }else {
            mapper.insert(visitor);
            return true;
        }

    }

}
